/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AutenticacionElector.java
 *
 * Created on 11-ene-2013, 16:41:18
 */
package sisvoto.vista;

import SecuGen.FDxSDK.jni.FDxDeviceInfoParam;
import SecuGen.FDxSDK.jni.FDxDeviceName;

import SecuGen.FDxSDK.jni.FDxPortAddr;
import SecuGen.FDxSDK.jni.JFPLib;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import sisvoto.DAO.ConsultasBD;
import sisvoto.Utiles.Mensajes;
import sisvoto.Utiles.Util;
import sisvoto.controlador.ValidacionFinger;
import sisvoto.modelo.EleccionActual;
import sisvoto.modelo.EleccionElector;
import sisvoto.modelo.Elector;
import sisvoto.modelo.HuellaDactilar;
import sisvoto.modelo.VotantesEleccion;

/**
 *
 * @author DJ2-PC
 */
public class AutenticacionElector extends javax.swing.JDialog {

    private Elector elector;
    private JFPLib fplib = null;
    private long ret;
    private FDxDeviceInfoParam deviceInfo = new FDxDeviceInfoParam();
    private long devicePort;
    EleccionActual eleccionactual;

    /** Creates new form AutenticacionElector */
    public AutenticacionElector(java.awt.Frame parent, boolean modal, EleccionActual eleccionactual) {
        super(parent, modal);
        this.elector = new Elector();
        this.eleccionactual = eleccionactual;
        initComponents();
    }

    public Elector getElector() {
        return elector;
    }

    public void setElector(Elector elector) {
        this.elector = elector;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        imagePanel1 = new com.mycsistemas.swingec.panel.ImagePanel();
        curvesPanel1 = new com.mycsistemas.swingec.panel.CurvesPanel();
        jLblAutElector = new javax.swing.JLabel();
        jLblAutCedula = new javax.swing.JLabel();
        jTxtAutCedula = new javax.swing.JTextField();
        jLbAutNombrel = new javax.swing.JLabel();
        jLblAutApellido = new javax.swing.JLabel();
        jLblAutAnioEdu = new javax.swing.JLabel();
        jTxtAutNombre = new javax.swing.JTextField();
        jTxtAutApellido = new javax.swing.JTextField();
        jTxtAutParalelo = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Autenticación Elector");
        setMinimumSize(new java.awt.Dimension(490, 340));

        imagePanel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sisvoto/Recursos/''_10102012_1738.jpeg"))); // NOI18N

        curvesPanel1.setLayout(null);

        jLblAutElector.setFont(new java.awt.Font("Georgia", 3, 18));
        jLblAutElector.setForeground(new java.awt.Color(255, 255, 255));
        jLblAutElector.setText("Autenticación de Elector");
        curvesPanel1.add(jLblAutElector);
        jLblAutElector.setBounds(120, 20, 290, 21);

        jLblAutCedula.setFont(new java.awt.Font("Elephant", 0, 14));
        jLblAutCedula.setForeground(new java.awt.Color(255, 255, 255));
        jLblAutCedula.setText("Cédula: ");
        curvesPanel1.add(jLblAutCedula);
        jLblAutCedula.setBounds(40, 60, 100, 40);

        jTxtAutCedula.setFont(new java.awt.Font("Courier New", 1, 18));

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${elector.cedula}"), jTxtAutCedula, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jTxtAutCedula.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTxtAutCedulaKeyPressed(evt);
            }
        });
        curvesPanel1.add(jTxtAutCedula);
        jTxtAutCedula.setBounds(140, 60, 280, 40);

        jLbAutNombrel.setFont(new java.awt.Font("Elephant", 0, 14));
        jLbAutNombrel.setForeground(new java.awt.Color(255, 255, 255));
        jLbAutNombrel.setText("Nombres:");
        curvesPanel1.add(jLbAutNombrel);
        jLbAutNombrel.setBounds(40, 120, 100, 30);

        jLblAutApellido.setFont(new java.awt.Font("Elephant", 0, 14));
        jLblAutApellido.setForeground(new java.awt.Color(255, 255, 255));
        jLblAutApellido.setText("Apellidos:");
        curvesPanel1.add(jLblAutApellido);
        jLblAutApellido.setBounds(40, 160, 100, 30);

        jLblAutAnioEdu.setFont(new java.awt.Font("Elephant", 0, 14));
        jLblAutAnioEdu.setForeground(new java.awt.Color(255, 255, 255));
        jLblAutAnioEdu.setText("Año de Educación:");
        curvesPanel1.add(jLblAutAnioEdu);
        jLblAutAnioEdu.setBounds(40, 200, 150, 30);

        jTxtAutNombre.setEditable(false);
        jTxtAutNombre.setFont(new java.awt.Font("Franklin Gothic Medium", 1, 18));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${elector.nombre}"), jTxtAutNombre, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        curvesPanel1.add(jTxtAutNombre);
        jTxtAutNombre.setBounds(140, 120, 280, 30);

        jTxtAutApellido.setEditable(false);
        jTxtAutApellido.setFont(new java.awt.Font("Franklin Gothic Medium", 1, 18));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${elector.apellido}"), jTxtAutApellido, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        curvesPanel1.add(jTxtAutApellido);
        jTxtAutApellido.setBounds(140, 160, 280, 30);

        jTxtAutParalelo.setEditable(false);
        jTxtAutParalelo.setFont(new java.awt.Font("Franklin Gothic Medium", 1, 18));
        curvesPanel1.add(jTxtAutParalelo);
        jTxtAutParalelo.setBounds(190, 200, 230, 30);

        jLabel1.setFont(new java.awt.Font("Tahoma", 2, 11));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel1.setText("Ingrese el número de Cédula y Presione Enter");
        curvesPanel1.add(jLabel1);
        jLabel1.setBounds(140, 100, 280, 14);

        javax.swing.GroupLayout imagePanel1Layout = new javax.swing.GroupLayout(imagePanel1);
        imagePanel1.setLayout(imagePanel1Layout);
        imagePanel1Layout.setHorizontalGroup(
            imagePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(curvesPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 482, Short.MAX_VALUE)
        );
        imagePanel1Layout.setVerticalGroup(
            imagePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(curvesPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 276, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(imagePanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(imagePanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jTxtAutCedulaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTxtAutCedulaKeyPressed
// TODO add your handling code here:
    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
        if (!jTxtAutCedula.getText().isEmpty()) {
            if (new Util().esCedulaValida(jTxtAutCedula.getText())) {
                ArrayList<Elector> electores = (ArrayList<Elector>) new ConsultasBD().buscarPOR("Elector", "cedula", "'" + jTxtAutCedula.getText() + "'");
                if (!electores.isEmpty()) {

                    this.elector = electores.get(0);
                    ArrayList<VotantesEleccion> votantes = (ArrayList<VotantesEleccion>) new ConsultasBD().buscarPOR2("VotantesEleccion", "elector", "'" + elector.getId() + "'", "eleccion", "'" + eleccionactual.getEleccion().getId() + "'");
                    if (votantes.isEmpty()) {
                        bindingGroup.unbind();
                        bindingGroup.bind();
                        jTxtAutParalelo.setText(elector.getAnioparalelo().getAnio().getNombre() + " " + elector.getAnioparalelo().getParalelo().getNombre());
                        ArrayList<EleccionElector> elecelec = (ArrayList<EleccionElector>) new ConsultasBD().buscarPOR("EleccionElector", "elector", "'" + elector.getId() + "'");
                        if (!elecelec.isEmpty()) {
                            ArrayList<HuellaDactilar> huellas = (ArrayList<HuellaDactilar>) new ConsultasBD().buscarPOR("HuellaDactilar", "elector", "'" + elector.getId() + "'");
                            if (huellas.size() == 2) {
                                if (Configuracionfinger.getFplib() == null) {
                                    conexionauxfinger();
                                }
                                ValidacionFinger vf = new ValidacionFinger(huellas, fplib, this, elector);
                                vf.start();
                            } else {
                                new Mensajes().StandarWarning(this, "Aún no Ha Registrado Sus Huellas");
                            }
                        } else {
                            this.elector = null;
                            new Mensajes().StandarWarning(this, "Éste Elector No Puede Sufragar en Ésta Elección. \n        Consulte con el Administrador");
                            this.dispose();
                        }
                    }else{
                        new Mensajes().StandarWarning(this, "Éste Elector Ya Ha Ejercido El Voto");
                        this.dispose();
                    }
                } else {
                    new Mensajes().StandarWarning(this, "Éste Elector No está Registrado. \n   Consulte Con el Administrador");
                }
            } else {
                new Mensajes().StandarWarning(this, "Verifique el Número de Cédula Ingresado");
            }
        } else {
            new Mensajes().StandarWarning(this, "Ingrese Su Número de Cédula ");
        }
    }
}//GEN-LAST:event_jTxtAutCedulaKeyPressed

    /**
     * @param args the command line arguments
     */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private com.mycsistemas.swingec.panel.CurvesPanel curvesPanel1;
    private com.mycsistemas.swingec.panel.ImagePanel imagePanel1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLbAutNombrel;
    private javax.swing.JLabel jLblAutAnioEdu;
    private javax.swing.JLabel jLblAutApellido;
    private javax.swing.JLabel jLblAutCedula;
    private javax.swing.JLabel jLblAutElector;
    private javax.swing.JTextField jTxtAutApellido;
    private javax.swing.JTextField jTxtAutCedula;
    private javax.swing.JTextField jTxtAutNombre;
    private javax.swing.JTextField jTxtAutParalelo;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    private void conexionauxfinger() {
        fplib = new JFPLib(FDxDeviceName.CN_FDU02);
        devicePort = FDxPortAddr.AUTO_DETECT;
        ret = fplib.openDevice(devicePort);
        ret = fplib.getDeviceInfo(deviceInfo);
        Configuracionfinger.setFplib(fplib);
    }
}
